import { onLoad, onShow } from "@dcloudio/uni-app"
import { ref } from "vue";
import {  moduleTypeRule, objectAny } from "../../options/base";
import { changeOrderStatus } from "../../utils/doCloud";
import { homeMenuToPage } from "../../utils/tabCommon";
import { toggleModule, verifyModuleList } from "../../utils/toggleModule";
import { baseCopy, goToPage, orderNavToRightPage, previewImage, showModal, showToast, toggleOrderStatus, togglePackStatus } from "../../utils/utils"

type orderRule={
    rider_order_status:0|1|2|3|4|5|6|7|8|9;
    show_time_out_ms:number;
    order_out_time:number;
    create_date:number;
    show_show_time_out_m:number;
    diff_count_down:number;
    toggleInfo:objectAny;
    menu_id:Array<objectAny>;
    rider_order_receipt:Array<objectAny>;
    school_id:Array<objectAny>;
    yizhan_id:Array<objectAny>;
    userAddress_id:Array<objectAny>;
    moduleName:moduleTypeRule
}

const _oderInfoDeafult:orderRule={
    rider_order_status:0,
    show_time_out_ms:900000,
    show_show_time_out_m:15,
    order_out_time:900000,
    create_date:0,
    diff_count_down:900000,
    toggleInfo:toggleOrderStatus(0),
    rider_order_receipt:[],
    moduleName:`runErrand`,
    school_id:[{
        school_name:`默认一统云数据`
    }],
    yizhan_id:[{
        name:`默认一统云数据`
    }],
    userAddress_id:[{
        address:`默认数据`,
        name:`默认数据`,
        moblie:`12345678912`
    }],
    menu_id:new Array(3).fill({
        packInfoList:new Array(3).fill({
            text:'默认数据',
            borderColor: "#f37000",
            color: "#f37000"
        })
    })
}

export default {
    setup(){
        let onLoadLoaddingEnd=false;   //是否在onLoad下数据已经加载完成
        // 结算模块 由哪里模块跳过来的
        let module:moduleTypeRule=`foodTakeout`;
        let orderId:string=``;
        const oderInfo=ref(_oderInfoDeafult);
        const loading = ref(true);
        
        Promise.all([
            new Promise<void>(resolve=>{
                onLoad(async (options)=>{
                    const queryModule:any=options.module||'';
                    module=queryModule as any;
                    orderId=options.id||'';
                    if(orderId==``||!verifyModuleList.includes(queryModule)){
                        await showModal({
                            showCancel: false,
                            title: `订单异常`,
                            content: `订单异常，请稍后再试`
                        })
                        return goToPage({
                            path:`/common/tabbar1/tabbar1`,
                            navType:`pushTab`
                        })
                    }
                    resolve();
                })
            }),
            new Promise<void>(resolve=>{
                onShow(()=>{
                    getOrderData();
                    resolve();
                })
            })
        ]).then(async ()=>{
            onLoadLoaddingEnd=true;
            await getOrderData();
            //加载完成
            loading.value=false;
        })


        const countDownEnd=()=>{
            console.log(`倒计时结束`);
            getOrderData();
        }

        const cancelOrder=async ()=>{
            const next= await showModal({
                title:`操作提示`,
                content:`您确定需要取消该订单？`
            })
            if(!next||orderId==''){
                return false
            }
            const {tableName,update}=toggleModule[module]
            const upStatus=await changeOrderStatus(tableName,orderId,update,true);

            if(upStatus){
                showToast(`取消成功`,`success`)
                getOrderData();
            }
        }

        // 加载该订单的数据详细信息
        const getOrderData=async ()=>{
            if(orderId==''||!onLoadLoaddingEnd){
                return console.log(`xxxxxx=> onLoad下数据未加载完成，取消本次onShow需要做的事情 <=xxxxxx`)
            }
            // 获取的需要执行的模块信息
            const moduleData=toggleModule[module]
            // 执行模块信息的获取订单函数 获取订单信息
            const orderData=await moduleData.getOrderDetails(orderId)
            if(orderData.rider_order_status===8){
                // 设置倒计时
                orderData.diff_count_down= orderData.order_out_time-orderData.serverNowTime
            }

            //获取订单超时差
            const diff_ms=orderData.order_out_time-orderData.create_date
            //设置获取订单超时差 毫秒显示
            orderData.show_time_out_ms=diff_ms;
            // 设置获取订单超时差 分钟显示
            orderData.show_show_time_out_m=diff_ms/60/1000;

            // 根本本地配置信息转换订单的提示语句
            const toggleInfo = toggleOrderStatus(orderData.rider_order_status)
            orderData.toggleInfo=toggleInfo;
            oderInfo.value=orderData as orderRule;
        }

        // 跳转到新的页面
        const goToOtherPage=()=>{
            const { moduleName } = oderInfo.value
            orderNavToRightPage(oderInfo.value,moduleName);
        
        }

        return {
            loading,
            oderInfo,
            module,
            previewImage,
            countDownEnd,
            toggleOrderStatus,
            goToOtherPage,
            togglePackStatus,
            baseCopy,
            goToPage,
            cancelOrder
        }
    }
}